<?php
/**
 * RegisterAction.class.php (前台会员注册模块)
 *
 * @package      	jgsat
 * @author          liuxun QQ:147613338 <admin@jgsat.cn>
 * @copyright     	Copyright (c) 2008-2011  (http://www.jgsat.com)
 * @license         http://www.jgsat.com/license.txt
 * @version        	jgsat企业网站管理系统 v2.1 2011-03-01 jgsat.cn $
 */
if(!defined("JGsat")) exit("Access Denied");
class RegisterAction extends BaseAction
{
	
	function _initialize()
    {
		parent::_initialize();
		$this->dao = M('User');
		$_GET =get_safe_replace($_GET);
		//if(empty($this->member_config['member_register'])) $this->error(L('close_reg'));
    }
    public function index()
    {

		if( cookie('auth')){
			$this->assign('forward','');
			$this->assign('jumpUrl','/');
			$this->success(L('login_ok'));
		}

       // print_r($_SESSION['tel_verify']);exit;
        $this->assign('title','注册');
		$this->assign('bcid',0);
        $this->display();
    }

    public function doreg()
    {
        $data=array();
        $groupid=get_safe_replace($_POST['groupid']);
        $password = get_safe_replace($_POST['password']);
        $mobile = get_safe_replace($_POST['mobile']);
        $mobileYzm = get_safe_replace($_POST['mobileyzm']);
        $name = get_safe_replace($_POST['username']);
        $openid = get_safe_replace($_POST['openid']);
        $sina_uid = get_safe_replace($_POST['sina_uid']);


        $pattern = "/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i";
        $emlie=preg_match( $pattern, $name ) ;

        $pattern_name="/^[a-zA-Z\u0391-\uFFE5]/i";
        $name_p=preg_match( $pattern_name, $name ) ;
        if($emlie){
            $this->error("不能用邮箱作为昵称!");
        }
        if( $name_p){
            $this->error("首位不能为数字!");
        }
        if($this->dao->where("username='".$name."'")->find()){
            $this->error("用户已存在!");
        }
        if($this->dao->where("mobile='".$mobile."'")->find()){
            $this->error("手机号已存在!");
        }
        if($groupid=='1'){
         $realname=$name;
            $data['realname']=$realname;
        }
        if($groupid=='2'){
            $nickname=$name;
        }
        if($groupid=='3'){
            $realname=$name;
            $data['realname']=$realname;

        }

        if(empty($mobile)){
            $this->error("手机不能为空!");
        }
        if(empty($nickname) && $groupid==2){
            $this->error("昵称不能为空!");
        }
        if(empty($realname) && $groupid==1){
            $this->error("真实姓名不能为空!");
        }
        if(empty($realname) && $groupid==3){
            $this->error("公司名称不能为空!");
        }
        if(empty($password)){
            $this->error("密码不能为空!");
        }
        if(empty($mobileYzm)){
            $this->error("验证码不能为空!");

        }

//print_r($data);exit;
        if( md5($mobileYzm) != $_SESSION['tel_verify']){

            $this->error(L('验证码错误'));
        }


        $time_cha=time()-$_SESSION['time_verify'];
        if($time_cha > '120'){
            $this->error(L('验证码已过期'));
        }





      if( $mobile !=  $_SESSION['tel_yz']){

          $this->error(L('手机号或验证码错误'));
      }
        $status = $this->member_config['member_registecheck'] ? 0 : 1;
        // $groupid = 6;

       //print_r( $nickname);exit;
        if($openid){
            $data['openid']=$openid;
        }
        if($sina_uid){
            $data['sina_uid']=$sina_uid;
        }
        $data['username']= $name;
        $data['avatar']='../Public/images/order3.png';
        //$data['email']=$email;
        $data['groupid']=$groupid;
        $data['nickname']=$nickname;

        $data['mobile']=$mobile;
        $data['login_count']=1;
        $data['createtime']=time();
        $data['updatetime']=time();
        $data['last_logintime']=time();
        $data['reg_ip']=get_client_ip();
        $data['status']=$status;
        $authInfo['password'] = $data['password'] = sysmd5($password);

        if($r=$this->dao->create($data)){
            if(false!==$this->dao->add()){
                $authInfo['id'] = $uid=$this->dao->getLastInsID();
                $authInfo['groupid'] = $ru['role_id']=$data['groupid'];
                $ru['user_id']=$uid;
                $roleuser=M('RoleUser');
                $roleuser->add($ru);
                $jgsat_auth_key = sysmd5($this->sysConfig['ADMIN_ACCESS'].$_SERVER['HTTP_USER_AGENT']);
                $jgsat_auth = authcode($authInfo['id']."-".$authInfo['groupid']."-".$authInfo['password'], 'ENCODE', $jgsat_auth_key);


                $authInfo['username'] = $data['username'];
                $authInfo['email'] = $data['email'];
                cookie('auth',$jgsat_auth,$cookietime);
                cookie('username',$authInfo['username'],$cookietime);
                cookie('groupid',$authInfo['groupid'],$cookietime);
                cookie('userid',$authInfo['id'],$cookietime);
                cookie('createtime',time(),$cookietime);


                $this->assign('jumpUrl',$this->forward);
                if($groupid==1){

                    $where="varname='member_sjs'";
                }
                if($groupid==2){

                    $where="varname='member_yz'";
                }

                if($groupid==3){

                    $where="varname='member_gys'";
                }

                $config = M('Config')->field('value')->where($where)->find();

                $arr['touserid']=$authInfo['id'];
                $arr['isnew']=1;
                $arr['title']=str_cut($config['value'],40);
                $arr['content']=$config['value'];
                $arr['lang']=1;
                $arr['tousername']=$authInfo['username'];
                $arr['createtime']=time();
                $mss=M('User_message')->add($arr);
                $usuccsrl='index.php?g=Mobile&m=User&a=index';
                $this->success(L('reg_ok'),$usuccsrl);
            }
            else{
                $this->error(L('reg_error'));
            }
        }
        else{
            $this->error($this->dao->getError());
        }

    }


	function checkEmail(){
        $email=$_GET['email'];
		$userid=intval($_GET['userid']);
		if(empty($userid)){
			if($this->dao->getByEmail($email)){
				 echo 'false';
			}else{
				echo 'true';
			}
		}else{
			//判断邮箱是否已经使用
			if($this->dao->where("id!={$userid} and email='{$email}'")->find()){
				 echo 'false';
			}else{
				echo 'true';
			}
		}
        exit;
	}

	function checkusername(){
            $username=$_GET['username'];
            if($this->dao->getByUsername($username)){
                echo 'false';
            }else{
                echo 'true';
            }
            exit;
	}
	function checkmobile(){
            $mobile=$_GET['mobile'];
            if($this->dao->where("mobile='".$mobile."'")->find()){
                echo 'false';
            }else{
                echo 'true';
            }
            exit;
	}
        
	function checknickname(){
            $nickname=$_GET['nickname'];
            if($this->dao->where("nickname='".$nickname."' and id<>".$this->_userid)->find()){
                echo 'false';
            }else{
                echo $this->_userid;
            }
            exit;
	}

//修改密码
    function edit(){

        if($_POST){
            $data=array();

            $mobile = get_safe_replace($_POST['mobile']);
            $mobileYzm = get_safe_replace($_POST['mobileyzm']);
            $name = get_safe_replace($_POST['username']);
           // $password=get_safe_replace($_POST['password']);
            //$password1=get_safe_replace($_POST['$password1']);



            $mobile_u=$this->dao->where("mobile='".$mobile."'")->find();
            if(empty($mobile_u)){
                $this->error("手机号不存在!");
            }

            if(empty($mobile)){
                $this->error("手机不能为空!");
            }

            if(empty($mobileYzm)){
                $this->error("验证码不能为空!");

            }

            $time_cha=time()-$_SESSION['time_verify'];
            if($time_cha > '120'){
                $this->error(L('时间太久已过期'));
            }

            if( md5($mobileYzm) != $_SESSION['tel_verify']){
              $this->error(L('验证码错误'));
            }
            if( $mobile !=  $_SESSION['tel_yz']){

                $this->error(L('手机号或验证码错误'));
            }
            $this->assign('mobileYzm',$mobileYzm);
            $this->assign('name',$name);
            $this->assign('mobile',$mobile);
            $this->display('edit_set');
        }else{

            $this->display();
        }


    }

    function save(){

        if($_POST){
            $data=array();

            $mobile = get_safe_replace($_POST['mobile']);
            $mobileYzm = get_safe_replace($_POST['mobileYzm']);
// $mobileYzm.'aaaaaa'. $mobile ;exit;
            $password=get_safe_replace($_POST['password']);
            //echo $name;exit;
            $password1=get_safe_replace($_POST['password1']);

            if(empty($password1)||empty($password)){
                $this->error("两次密码都不可为空!");
            }
            if($password1 != $password){
                $this->error("两次密码不一致!");
            }



            $mobile_u=$this->dao->where("mobile='".$mobile."'")->find();
            if(empty($mobile_u)){
                $this->error("错误，手机号码不正确!");
            }

            $time_cha=time()-$_SESSION['time_verify'];
            if($time_cha > '600'){
                $this->error(L('时间太久已过期'));
            }

            if( md5($mobileYzm) != $_SESSION['tel_verify']){
                $this->error("错误，验证码不正确!");
            }
            if( $mobile !=  $_SESSION['tel_yz']){
               $this->error("错误，手机号码不正确!");
            }
            $data["password"]=sysmd5($password);
            $result = M("User")->where("mobile=".$mobile)->save($data);

            cookie(null,'YP_');
           if($result){
               $this->success('修改成功',U('Mobile/Index/index'));
              // $this->assign('jumpUrl',U('Mobile/Login/index'));
           }

        }else{
            $this->success();
        }

    }
	


}
?>